package top.lionkliu.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;
import top.lionkliu.entity.User;
import top.lionkliu.mapper.UserMapper;
import top.lionkliu.service.UserService;
import top.lionkliu.utils.MybatisUtil;

import java.util.List;

/**
 * @author : lyj
 * @Timer : 2022/5/11
 * @Description :
 */
@Slf4j
public class UserServiceImpl implements UserService {


    @Override
    public List<User> selectAll() {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = mapper.selectAll();
        //sqlSession.close();
        log.info("查询所有用户：{}", users);
        MybatisUtil.closeSqlSession();
        return users;
    }

    @Override
    public Boolean login(User user) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        Boolean login;
        if (mapper.login(user) == null) {
            login = false;
        } else {
            login = mapper.login(user);
        }
        User user1 = mapper.selectByName(user.getName());
        log.info("用户:{}登录成功", user1.getName());
        MybatisUtil.closeSqlSession();
        return login;
    }

    @Override
    public void register(User user) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.register(user);
        sqlSession.commit();
        User user1 = mapper.selectByName(user.getName());
        log.info("用户:{}注册成功", user1.getName());
        MybatisUtil.closeSqlSession();
    }

    @Override
    public Boolean checkUser(User user) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        Boolean checkUser;
        if (mapper.checkUser(user) == null) {
            checkUser = false;
        } else {
            checkUser = mapper.checkUser(user);
        }
        log.info("用户:{}是否存在：{}", user.getName(),checkUser);
        MybatisUtil.closeSqlSession();
        return checkUser;
    }

    @Override
    public User selectById(int id) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.selectById(id);
        log.info("工具id:{}查询到用户：{}",id, user);
        MybatisUtil.closeSqlSession();
        return user;
    }

    @Override
    public void delete(int id) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        log.info("删除用户：{}",mapper.selectById(id));
        mapper.delete(id);
        sqlSession.commit();
        MybatisUtil.closeSqlSession();
    }

    @Override
    public void update(User user) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.update(user);
        sqlSession.commit();
        log.info("修改用户:{}",user);
        MybatisUtil.closeSqlSession();
    }

    @Override
    public User selectByName(String name) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.selectByName(name);
        log.info("根据姓名查询用户：{}",user);
        MybatisUtil.closeSqlSession();
        return user;
    }

    @Override
    public void updateUserInfo(User user) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.updateUserInfo(user);
        sqlSession.commit();
        log.info("修改用户信息：{}",user);
        MybatisUtil.closeSqlSession();
    }
}
